package marseillaisdu90.student_life;

import java.util.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class NouvellePartie extends Activity{

	private Button boutonSuivant;
	private ImageView fondEcran;
	private final int CODE_ACTIVITE_FENETRE_JEU = 1;
	private MonApplication appli;
	private EditText identifiant;
	private Spinner personnage;
	private LinearLayout layout;
	private String[] liste_perso;
	private int[] liste_icon;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		appli = (MonApplication)getApplication();
        appli.setActivitePricipale(this);
        appli.afficherMessageBienvenu();
        
		setContentView(R.layout.nouvelle_partie);
				
		boutonSuivant = (Button)findViewById(R.id.bouton_suivant);
		fondEcran = (ImageView)findViewById(R.id.fond_ecran);
		identifiant = (EditText)findViewById(R.id.identifiant);
		personnage = (Spinner)findViewById(R.id.personnage);
		layout = (LinearLayout)findViewById(R.id.layout_id_perso);
		
		boutonSuivant.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				appli.incNumImageIntro();
				
				if(appli.getNumImageIntro() == 5) {
					appli.modele.joueur.setNom(identifiant.getText().toString());
					appli.modele.joueur.setImage(liste_perso[personnage.getSelectedItemPosition()]);
				}
				mettreAJourImage();
				
			}
		});
		
		
		liste_perso = new String[]{ "aveugle", "barake", "chapeau", "costard", "karate", "scientifique" };
		liste_icon = new int[]{ R.drawable.bas_aveugle, R.drawable.bas_barake, R.drawable.bas_chapeau, R.drawable.bas_costard, R.drawable.bas_karate, R.drawable.bas_scientifique };
		
		ArrayList<HashMap<String, String>> listSpinner = new ArrayList<HashMap<String, String>>();

		HashMap<String, String> map;
		
		for(int i = 0; i< liste_perso.length; i++) {
			map = new HashMap<String, String>();
			map.put("icon", String.valueOf(liste_icon[i]));
			map.put("perso", liste_perso[i]);
			listSpinner.add(map);
		}		
		
		SimpleAdapter adapter = new SimpleAdapter(this.getBaseContext(), listSpinner, R.layout.row,
				new String[] { "icon", "perso" }, new int[] {R.id.icon, R.id.perso });
		
		//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		personnage.setAdapter(adapter);
		
		/*
		if(appli.aDejaEteLance) {
			Intent intent = new Intent(this, StudentLife.class);
			//Bundle objetbunble = new Bundle();
			//objetbunble.putLong("id", identifiantTache);
			//intent.putExtras(objetbunble);
			startActivityForResult(intent, CODE_ACTIVITE_FENETRE_JEU);
			//transitionActivity();
			
			appli.setNumImageIntro(1);
		}
		*/
		
		mettreAJourImage();
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch(requestCode){
			case CODE_ACTIVITE_FENETRE_JEU :
				mettreAJourImage();
				break;
			default : 
				break;
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	public void mettreAJourImage() {
		
		if(appli.getNumImageIntro() == 4)
			layout.setVisibility(View.VISIBLE);
		else
			layout.setVisibility(View.INVISIBLE);
		
		if(appli.getNumImageIntro() <= 5)
			fondEcran.setImageResource(getResources().getIdentifier("intro" + appli.getNumImageIntro(), "drawable", getPackageName()));
		else {
			Intent intent = new Intent(boutonSuivant.getContext(), StudentLife.class);
			//Bundle objetbunble = new Bundle();
			//objetbunble.putLong("id", identifiantTache);
			//intent.putExtras(objetbunble);
			startActivityForResult(intent, CODE_ACTIVITE_FENETRE_JEU);
			//transitionActivity();
			
			appli.setNumImageIntro(1);
		}
		
	}
	
}
